package com.ferenc.poi.tl;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author <a href="mailto:ferenc.zeng@gmail.com">Ferenc Zeng</a>
 * @since 2022-01-16
 */
public class WordGeneratorUtils {

    public static void main(String[] args) throws IOException {

        long start = System.currentTimeMillis();

        generateWord();

        System.out.println(System.currentTimeMillis() - start);
    }

    public static void generateWord() throws IOException {


        Map<String, Object> datas = new HashMap<String, Object>();
        // text
        datas.put("城市", "北京市");
        datas.put("startDate", "2020-09-17");
        datas.put("endDate", "2020-10-16");
        datas.put("totCnt", 3638763);
        datas.put("totAmt", "6521");
        datas.put("onCnt", 2874036);
        datas.put("onAmt", "4768");
        datas.put("offCnt", 764727);
        datas.put("offAmt", "1753");
        datas.put("typeCnt", 36);

        List<Goods> goods = new ArrayList<Goods>();
        Goods goods1 = new Goods();
        goods1.setNum(1);
        goods1.setType("臭美毁肤");
        goods1.setSv(675512);
        goods1.setSa("589");
        Goods goods2 = new Goods();
        goods2.setNum(2);
        goods2.setType("女装");
        goods2.setSv(602145);
        goods2.setSa("651");
        Goods goods3 = new Goods();
        goods3.setNum(3);
        goods3.setType("手机");
        goods3.setSv(587737);
        goods3.setSa("866");
        Goods goods4 = new Goods();
        goods4.setNum(4);
        goods4.setType("家具家装");
        goods4.setSv(551193);
        goods4.setSa("783");
        Goods goods5 = new Goods();
        goods5.setNum(5);
        goods5.setType("食物饮品");
        goods5.setSv(528604);
        goods5.setSa("405");
        goods.add(goods1);
        goods.add(goods2);
        goods.add(goods3);
        goods.add(goods4);
        goods.add(goods5);


        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();

        Configure config = Configure.builder()
                .bind("goods", policy).bind("labors", policy).build();


        datas.put("goods", goods);



//        // hyperlink
//        datas.put("introduce", Texts.of("http://www.deepoove.com").link("http://www.deepoove.com").create());
//        // picture
//        datas.put("portrait", Pictures.ofLocal("src/test/resources/sayi.png").size(60, 60).create());
//        // table
//        datas.put("solution_compare", table);
//        // numbering
//        datas.put("feature",
//                Numberings.create("Plug-in grammar, add new grammar by yourself",
//                        "Supports word text, local pictures, web pictures, table, list, header, footer...",
//                        "Templates, not just templates, but also style templates"));

        //获取Word模板，模板存放路径在项目的resources目录下
        InputStream ins = WordGeneratorUtils.class.getResourceAsStream("/template.docx");
        XWPFTemplate.compile(ins, config).render(datas)
                .writeToFile("./out_template.docx");



//        //获取Word模板，模板存放路径在项目的resources目录下
//        InputStream ins = WordGenerateUtils.class.getResourceAsStream("/template.docx");
//        //注册xdocreport实例并加载FreeMarker模板引擎
//        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
//                TemplateEngineKind.Freemarker);
//        //创建xdocreport上下文对象
//        IContext context = report.createContext();
//        //创建要替换的文本变量
//        context.put("city", "北京市");
//        context.put("startDate", "2020-09-17");
//        context.put("endDate", "2020-10-16");
//        context.put("totCnt", 3638763);
//        context.put("totAmt", "6521");
//        context.put("onCnt", 2874036);
//        context.put("onAmt", "4768");
//        context.put("offCnt", 764727);
//        context.put("offAmt", "1753");
//        context.put("typeCnt", 36);
//        List<Goods> goodsList = new ArrayList<Goods>();
//        Goods goods1 = new Goods();
//        goods1.setNum(1);
//        goods1.setType("臭美毁肤");
//        goods1.setSv(675512);
//        goods1.setSa("589");
//        Goods goods2 = new Goods();
//        goods2.setNum(2);
//        goods2.setType("女装");
//        goods2.setSv(602145);
//        goods2.setSa("651");
//        Goods goods3 = new Goods();
//        goods3.setNum(3);
//        goods3.setType("手机");
//        goods3.setSv(587737);
//        goods3.setSa("866");
//        Goods goods4 = new Goods();
//        goods4.setNum(4);
//        goods4.setType("家具家装");
//        goods4.setSv(551193);
//        goods4.setSa("783");
//        Goods goods5 = new Goods();
//        goods5.setNum(5);
//        goods5.setType("食物饮品");
//        goods5.setSv(528604);
//        goods5.setSa("405");
//        goodsList.add(goods1);
//        goodsList.add(goods2);
//        goodsList.add(goods3);
//        goodsList.add(goods4);
//        goodsList.add(goods5);
//        context.put("goods", goodsList);
//        //创建字段元数据
//        FieldsMetadata fm = report.createFieldsMetadata();
//        //Word模板中的表格数据对应的集合类型
//        fm.load("goods", Goods.class, true);
//        //输出到本地目录
//        FileOutputStream out = new FileOutputStream(new File("D:/template-1.docx"));
//        report.process(context, out);
    }
}
